import processing.serial.*;

Serial port;
PFont myFont;
String inString="";
String buffer="";
int cnt=0;

PrintWriter outfile;

static int DATASIZE = 50;
int[] data = new int[DATASIZE];
String[] tag = new String[DATASIZE];

static int chartX = 120;
static int chartY = 20;
static int chartWidth = 500;
static int chartHeight = 400;

static int midBaro=1000;
static int defaultMaxBaro=1010;
static int defaultMinBaro=990;

color lineColor = color(0, 255,0 );
color msgColor = color(255,0,0);

int maxdata=0;
int mindata=0;




void setup(){
  myFont = loadFont("SansSerif.plain-18.vlw"); 
  textFont(myFont, 18); 

  println(Serial.list());
  size (640,480);
  // Assuming that 3rd serial port is connected to Arduino
  port = new Serial(this, Serial.list()[2], 9600); 
  port.bufferUntil(10);
  frameRate(1);


  for ( int i =0 ; i < DATASIZE ; i++){
    data[i]=0;
  }

  outfile = createWriter("data.txt");


  // DEBUG
  cnt = 17;
  buffer = "[2010/5/12 05:59:59]  995 hPa 25 C\n" + 
    "[2010/5/12 06:59:59]  995 hPa 25 C\n" + 
    "[2010/5/12 07:59:58]  995 hPa 25 C\n" + 
    "[2010/5/12 08:59:58]  995 hPa 25 C\n" + 
    "[2010/5/12 09:59:57]  995 hPa 25 C\n" + 
    "[2010/5/12 10:59:57]  994 hPa 25 C\n" + 
    "[2010/5/12 11:59:56]  994 hPa 25 C\n" + 
    "[2010/5/12 12:59:56]  993 hPa 25 C\n" + 
    "[2010/5/12 13:59:55]  993 hPa 25 C\n" + 
    "[2010/5/12 14:59:55]  994 hPa 25 C\n" + 
    "[2010/5/12 15:59:55]  994 hPa 26 C\n" + 
    "[2010/5/12 16:59:54]  995 hPa 26 C\n" + 
    "[2010/5/12 17:59:54]  995 hPa 26 C\n" + 
    "[2010/5/12 18:59:53]  996 hPa 25 C\n" + 
    "[2010/5/12 19:59:53]  996 hPa 25 C\n" + 
    "[2010/5/12 20:59:52]  997 hPa 25 C\n" + 
    "[2010/5/12 21:59:52]  997 hPa 25 C\n";

  addData(1010,"");
  addData(1000, "");
  addData(996, "");
  addData(996, "");
  addData(995, "");
  addData(995, "");
  addData(994, "");
  addData(994, "");
  addData(1000, "");
  addData(993, "");
  addData(993, "");
  addData(994, "");
  addData(994, "");
  addData(995, "");
  addData(995, "");
  addData(995, "");
  addData(995, "");


  // DEBUG END






}
void draw(){
  int pos_y;
  background(128);

  // draw graph area
  noStroke();
  fill(196);
  rect( chartX, chartY, chartWidth, chartHeight);

  // draw graph
  drawChart();




  // display message log (over-wrapped)
  if ( cnt < 18 ) {
    pos_y = 15;
  } 
  else {
    pos_y = 15 - (cnt-17) * 22;
  }
  fill(0);
  text(buffer, 30, pos_y);

}


void serialEvent(Serial p) { 
  buffer += "[";

  inString = (port.readString()); 
  int ss = second();  // Values from 0 - 59
  int mm = minute();  // Values from 0 - 59
  int hh = hour();    // Values from 0 - 23
  int dd = day();    // Values from 1 - 31
  int mo = month();  // Values from 1 - 12
  int yyyy = year();   // 2003, 2004, 2005, etc.
  buffer += String.valueOf(yyyy);
  buffer += "/";
  buffer += String.valueOf(mo);
  buffer += "/";
  buffer += String.valueOf(dd);
  buffer += " ";
  buffer += String.format("%02d", hh);
  buffer += ":";
  buffer += String.format("%02d", mm);
  buffer += ":";
  buffer += String.format("%02d", ss);
  buffer += "]  ";
  buffer += inString;
  cnt++;
  print(buffer);
  String msg=String.format("%02d", mo)  + "/" +String.format("%02d", dd);
  msg += " " + String.format("%02d", hh)  + ":" +String.format("%02d", mm);

  String[] s=split(inString, " ");
  addData(int(s[0]),msg);

}

void addData(int hpa, String msg) {
  for ( int i=DATASIZE-1 ; i > 0 ; i--){
    data[i]=data[i-1];
    tag[i] = tag[i-1];
  }
  data[0] =hpa;
  tag[0] = msg;

  maxdata = data[0] ; 
  mindata = data[0] ;

  for ( int i = 1 ; i < DATASIZE ; i++) {
    if ( data[i] > maxdata ){
      maxdata = data[i];
    }
    if (data[i] < mindata){
      mindata = data[i];
    }
  }

  outfile.println(hpa);
  outfile.flush();



}


void drawChart(){
  stroke(lineColor);
  noFill();
  strokeWeight(3);
  println("---------------------------");
  beginShape();
  for (int i = 0 ; i <cnt ; i++){
    if (i >= DATASIZE ) break;

    float x = chartX + chartWidth - (i * chartWidth / DATASIZE);
    float y = chartY + (defaultMaxBaro - data[i]) * chartHeight / (defaultMaxBaro - defaultMinBaro);
    print(data[i] + " ");
    print(x);
    print(" ");
    println(y);
    vertex( x ,y );

    if ( i-1 == cnt || i == 0) 
      vertex( x ,y );

  }

  endShape();
}



